import { Module } from 'vuex'
import { doctorlistDoctorReq, doctorSearchDoctorReq, doctorGetDnDaylineReq, doctorEditDoctorReq, doctorEditDnDaylineReq } from '@/api/doctor.api'
import { ElMessage } from 'element-plus'
import qs from 'qs'
type doctype = {
    total: number | null
    doctorList: []
    doctorDay: {
        head: string,
        date: [],
        department: string,
        name: string
    },
}

const docModule: Module<doctype, any> = {
    // vuex 可定要提供一个接口，用来描述每一个模块
    state: {
        total: null,
        doctorList: [],
        //医生排班
        doctorDay: {
            head: '/1667806938017.webp',
            date: [],
            department: '',
            name: ''
        },
    },
    namespaced: true,
    mutations: {
        // total数据
        M_total(state, payload) {
            state.total = payload
        },
        // 医生综合列表数据
        M_doctorList(state, payload) {
            state.doctorList = payload
        },
        // 医生排班数据
        M_doctorDay(state: any, payload: {}) {
            state.doctorDay = payload
        },
    },
    actions: {
        // 医生综合列表
        async A_getDocList({ commit }, payload) {
            const res = await doctorlistDoctorReq(payload)

            if (res.data.code === 1) {
                commit('M_doctorList', res.data.data)
                commit('M_total', res.data.total)
            } else {
                return res.data.msg
            }
        },
        // 医生搜索
        async A_onSubmit({ commit }, payload) {

            const res = await doctorSearchDoctorReq(payload)
            console.log(res);
            if (res.data.code === 1) {
                commit('M_doctorList', res.data.data)
                commit('M_total', res.data.total)
            } else {
                return res.data.msg
            }

        },
        // 修改医生个人信息
        async A_ediForm({ commit }, payload) {
            const res = await doctorEditDoctorReq(payload)
            if (res.data.code === 1) {
                ElMessage({
                    message: res.data.msg,
                    type: 'success',
                })
            } else {
                ElMessage.error(res.data.msg)
            }

        },
        //排班  获取医生 护士排班 
        async A_getDoctor({ commit }, payload: any) {
            let res = await doctorGetDnDaylineReq(payload)
            if (res.data.data[0].date.indexOf('%') == -1) {
                res.data.data[0].date = JSON.parse(res.data.data[0].date)
                commit('M_doctorDay', res.data.data[0])
            } else {
                let Arr = res.data.data[0]
                Arr.date = Object.values(qs.parse(res.data.data[0].date))
                commit('M_doctorDay', Arr)
            }
        },
        // 修改排班
        async A_rosteringDoctor({ commit }, payload: any) {
            console.log(payload,);

            let res = await doctorEditDnDaylineReq(payload)
            console.log(res);

        },
    }
}

export default docModule